package com.xtaller.sdboot.controller;

import com.xtaller.sdboot.entity.IamModule;
import com.xtaller.sdboot.service.IamModuleService;
import com.xtaller.sdboot.utils.annotation.Permission;
import com.xtaller.sdboot.core.base.BaseController;
import com.xtaller.sdboot.core.base.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.Serializable;

/** 
* 代码生成 
* @author: Taller  
* @date: 2020-02-07 15:56:32
* @Description: 模块控制器 
*/ 
@RestController 
@RequestMapping("/v1/iam/module") 
public class IamModuleController extends BaseController<IamModule> {
    @Autowired private IamModuleService moduleService;

    @ApiOperation("读取模块表格")
    @Permission(authorities = "iam:module:data:list")
    @Override
    public Object getByPage(HttpServletRequest request) throws Exception {
        return super.getByPage(request);
    }

    @ApiOperation("创建")
    @Permission(authorities = "iam:module:opt:create")
    @Override
    public Object createEntity(@Valid @RequestBody IamModule entity, HttpServletRequest request) throws Exception {
        return super.createEntity(entity, request);
    }

    @ApiOperation("修改")
    @Permission(authorities = "iam:module:opt:update")
    @Override
    public Object updateEntity(@PathVariable("id") Serializable id, @Valid @RequestBody IamModule entity, HttpServletRequest request) throws Exception {
        return super.updateEntity(id, entity, request);
    }

    @ApiOperation("删除")
    @Permission(authorities = "iam:module:opt:delete")
    @Override
    public Object deleteEntity(@PathVariable("id") Serializable id, HttpServletRequest request) throws Exception {
        return super.deleteEntity(id, request);
    }

    @Override
    protected String beforeCreate(BaseEntity entity) throws Exception {
        var model = (IamModule) entity;
        var qw = new QueryWrapper<IamModule>();
        qw.lambda().and(x -> x.eq(IamModule::getCode, model.getCode())
            .or().eq(IamModule::getName, model.getName()));
        if(moduleService.get(qw) != null){
            return "模块编码或名称不能重复";
        }
        return super.beforeCreate(entity);
    }

    @Override
    protected String beforeUpdate(BaseEntity entity) throws Exception {
        var model = (IamModule) entity;
        var qw = new QueryWrapper<IamModule>();
        qw.lambda().and(x -> x.eq(IamModule::getCode, model.getCode())
                .or().eq(IamModule::getName, model.getName()))
                .ne(BaseEntity::getId, model.getId());
        if(moduleService.get(qw) != null){
            return "模块编码或名称不能重复";
        }
        return super.beforeUpdate(entity);
    }
}
